/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.bootstrap.client.ui;

import com.cleangwt.uibinder.client.TemplateSupport;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A panel for listing items with styles.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class ListGroup extends ComplexPanel {

  private static final String DEFAULT_STYLE = "list-group";
  private static final String ITEM_STYLE = "list-group-item";
  private static final String ACTIVE_STYLE = "active";

  /**
   * Creates a button group that wraps an existing &lt;ul&gt; element of template.
   * 
   * @param template the template
   * @param element the element to be wrapped
   */
  public static ListGroup wrap(TemplateSupport template, Element element) {
    ListGroup widget = new ListGroup(element);
    template.wrap(widget);
    return widget;
  }

  /**
   * Creates a list group
   */
  public ListGroup() {
    this.setElement(Document.get().createULElement());
    this.setStyleName(DEFAULT_STYLE);
  }

  /**
   * This constructor may be used by subclasses to explicitly use an existing element. This element
   * must be a &lt;div&gt; element.
   * 
   * @param element the element to be used
   */
  protected ListGroup(Element element) {
    assert element.getTagName().equalsIgnoreCase(DivElement.TAG);
    this.setElement(element);
  }

  /**
   * Sets the specified item's style to active.
   * 
   * @param index the index of item
   */
  public void setActive(int index) {
    Element e = this.getElement().getChild(index).cast();
    e.addClassName(ACTIVE_STYLE);
  }

  /**
   * Adds a list item with the given content.
   * 
   * @param content the content
   */
  public void addItem(String content) {
    prepareListItem().setInnerHTML(content);
  }

  /**
   * Adds a list item with the given widget.
   * 
   * @param widget widget
   */
  public void addItem(Widget widget) {
    this.add(widget, prepareListItem());
  }

  /**
   * Adds a list item with the given caption and content.
   * 
   * @param caption the caption
   * @param content the content
   */
  public void addItem(String caption, String content) {
    Element listItem = prepareListItem();
    Element captionElem = Document.get().createHElement(4);
    Element contentElem = Document.get().createPElement();
    captionElem.setInnerHTML(caption);
    contentElem.setInnerHTML(content);
    listItem.appendChild(captionElem);
    listItem.appendChild(contentElem);
  }

  private Element prepareListItem() {
    Element e = Document.get().createLIElement();
    e.setClassName(ITEM_STYLE);
    getElement().appendChild(e);
    return e;
  }
}
